//
//  XQWelcomeController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/19.
//

#import "XQWelcomeController.h"
#import "XQWelcomeCell.h"
#import "XQHomeTabBarController.h"

@interface XQWelcomeController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,copy) NSArray *images;

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation XQWelcomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self makeUI];
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)initData {
    self.selectIndex = 0;
    NSMutableArray *tempArr = [NSMutableArray array];
    for (int i = 1; i <= 3; i++) {
        NSString *imageName = [NSString stringWithFormat:@"welcome%d",i];
        UIImage *image = [UIImage imageNamed:imageName];
        [tempArr addObject:image];
    }
    self.images = tempArr;
}

- (void)makeUI {
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsZero;
    layout.itemSize = CGSizeMake([UIScreen mainScreen].width, [UIScreen mainScreen].height);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.automaticallyAdjustsScrollIndicatorInsets = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.collectionView registerClass:[XQWelcomeCell class] forCellWithReuseIdentifier:@"XQWelcomeCell"];
    [self.view addSubview:self.collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.images.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XQWelcomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XQWelcomeCell" forIndexPath:indexPath];
    cell.imageView.image = self.images[indexPath.item];
    [cell setIsLast:indexPath.item == self.images.count - 1];
    @weakify(self)
    cell.nextHandle = ^{
        @strongify(self);
        [self clickAtIndexPath:indexPath];
    };
    return  cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return  1;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    double dx = scrollView.contentOffset.x;
    NSInteger index = dx / [UIScreen mainScreen].width;
    self.selectIndex = index;
}

- (void)clickAtIndexPath:(NSIndexPath *)indexPath {
    self.selectIndex ++;
    if (self.selectIndex == self.images.count) {
        XQHomeTabBarController *mainVC = [[XQHomeTabBarController alloc] init];
        [self.navigationController setViewControllers:@[mainVC] animated:NO];
        
        [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"showGuide"];
        return;
    }
    
    [self.collectionView setContentOffset:CGPointMake(self.selectIndex * [UIScreen mainScreen].width,0) animated:YES];
}


@end
